import http from './interface'
import store from '../../store/index.js'
export const request = (data) => {
	//console.log("request=>data:",data);
	/*设置请求前拦截器*/
	http.interceptor.request = (config) => {		
		// console.log("请求前拦截",config);
		// uni.removeStorageSync('token')
	}
	//设置请求结束后拦截器
	http.interceptor.response = async (response) => {
		console.log("响应拦截：",response);
		let res = response.data
		if (res.code == 203) {
			//uni.removeStorageSync('token')
			console.log(uni.getStorageSync('token'))
			if (uni.getStorageSync('userId')) {
				await doRequest()
				// let firstData = response.firstData
				// const resold = await http.request({
				// 	url: firstData.url,
				// 	data: firstData.data
				// })
				// response = resold
			}
		} else if (res.code == 209 || res.code == 2001) {
			http.request({
				url: "/user/logout",
				data: {
					"scene": 1,
					"userId": uni.getStorageSync('userId')
				}
			}).then((result) => {
				// uni.showToast({
				// 	title: result.data.msg,
				// 	icon: 'none',
				// 	duration: 2000
				// });
			}).catch(() => {
		
			})
			uni.removeStorageSync('token')
			uni.removeStorageSync('userId')
			uni.reLaunch({
				url: 'subpageB/common/login/login1'
			})
		
		}
		return response
	}
	return http.request(data)
}
async function doRequest() {
	let res = await http.request({
		url: "/user/login",
		data: {
			userId: uni.getStorageSync('userId'),
			did: uni.getStorageSync('did'),
			loginType: 'autoLogin'
		}
	})
	let result = res.data
	if (result.code == 200) {
		uni.setStorageSync('token', result.data.loginToken)
	}
}
export default {
	request
}
